import scala.util.Random

object SodokuProducer {
	type Box = Array[Array[Int]]
	type Sodoku = Array[Array[Box]]

	def initBox: Box = {
		val init_vec = Random.shuffle(1 to 9)
		var box = Array.fill(3, 3)(0)
		for( x <- 0 until box.length; y <- 0 until box(x).length) {
			box(x)(y) = init_vec(x*3 + y)
	  }
	  box
	}

	def swapBoxRow(box: Box, method: Int): Box = method match {
		case 1 => Array(box.last) ++ box.init
		case 2 => box.tail ++ Array(box.head)
		case _ => box
	}

	def swapBoxCol(box: Box, method: Int): Box = method match {
		case 1 => box.map(row => Array(row.last) ++ row.init)
		case 2 => box.map(row => row.tail ++ Array(row.head))
		case _ => box
	}

	def printBox(box: Box): Unit = box.map(x => println(x.mkString(" ")))

	def main(args: Array[String]): Unit = {

		val box = initBox
		var sodoku: Sodoku = Array.fill(3, 3)(box)
		val method = (Random.nextInt(2) + 1)

		sodoku(1)(0) = swapBoxRow(box, method)
		sodoku(1)(2) = swapBoxRow(box, if (method == 1) 2 else 1)

		for (col <- 0 until 3; b = sodoku(1)(col)) {
			val m = (Random.nextInt(2) + 1)
			sodoku(0)(col) = swapBoxCol(b, m)
			sodoku(2)(col) = swapBoxCol(b, if (m == 1) 2 else 1)
		}

		for (sodokuRow <- 0 until 3; boxRow <- 0 until 3) {
			for(box <- 0 until 3) {
				print(sodoku(sodokuRow)(box)(boxRow).mkString(" ")); print(" ")
			}
			println("")
		}
	}
	
}